package com.lanou3g.code0423;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * Created by dllo on 18/4/23.
 */
public class SendClient {
    public static void main(String[] args) {
        try {
            //暂时不需要接收数据,所以随机绑定一个端口号即可
            DatagramSocket ds = new DatagramSocket();
            String s= "HelloWorld";
            byte[]buf=s.getBytes();//获取一数据数组
            DatagramPacket dp = new DatagramPacket(buf,buf.length,
            //要发送到的目的地 地址,端口号
            new InetSocketAddress("255.255.255.255",7788));
            ds.send(dp);//发送
           byte[]bytes=new byte[1024];
            DatagramPacket packet= new DatagramPacket(bytes,bytes.length);
            ds.receive(packet);//等着接收数据
            String data= new String(packet.getData(),0,packet.getLength());
            System.out.println(data);//显示数据
            ds.close();
            ds.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
